package com.example.springboot3.controller;

import com.example.springboot3.model.User;
import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.UUID;

@Controller
@Slf4j
@ResponseBody
public class HiController {

    @RequestMapping("/hi")
    public String sayHi(String name){
        log.info("hi:" + name);
        return "hi: " + name;
    }
    @GetMapping("/get")
    public User user(@RequestBody User user){
        return user;
    }
    @GetMapping("requestParam")
    public String time(@RequestParam(value = "time",required = false) String timePlus){
        return "test: " + timePlus;
    }
    @GetMapping("/p/{name}/{age}")
    public String urlVariable(@PathVariable String name,@PathVariable Integer age){
        return "姓名: " + name + ",年龄:" + age;
    }
    @PostMapping("/file")
    public String file(@RequestPart("myFile") MultipartFile file) throws IOException {
        file.transferTo(new File("G://springboot//aaa.jpg"));
        return file + "上传成功";
    }
    //拿到文件路径
    @Value("${upDown}")
    public String path;
    @PostMapping("filePlus")
    public String filePlus(@RequestPart("myFile") MultipartFile file) throws IOException {
        //生成唯一标识 UUID
        String uuid = UUID.randomUUID() + "";
        System.out.println(uuid);
        //拿到文件名字
        String name = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        System.out.println(path+uuid+name);
        file.transferTo(new File(path+uuid+name));
        return uuid+name;
    }
    @GetMapping("getHeader")
    public String getHeader(@RequestHeader("User-Agent") String header){
        return header;
    }
    @GetMapping("getHeader2")
    public Cookie[] getHeader2(HttpServletRequest request, HttpServletResponse response){
        //String str = request.getHeader("User-Agent");
        Cookie[] cookies = request.getCookies();
        return cookies;
    }
    @GetMapping("getCookie")
    public String getCookie(@CookieValue("test") String str){
        return str;
    }
    @GetMapping("setSession")
    public String setSession(HttpServletRequest request){
        HttpSession httpSession = request.getSession(true);
        httpSession.setAttribute("userName","haha");
        return "设置成功";
    }
    @GetMapping("getSession")
    public String getSession(@SessionAttribute(value = "userName",required = false) String userNmae){
        return userNmae;
    }
    @GetMapping("calc")
    public String calc(Integer sum1,Integer sum2){
        return "<h1> 结果为: " + (sum1 + sum2) + "</br>" +"<a href='javascript:history.go(-1);'>返回</a>" + "</h1>";
    }
}
